package day9;

import java.io.*;
import java.net.Socket;

public class FileThread extends Thread{
    private final Socket s;

     public static final String Folder ="E:\\java_workspace\\hbuilderproj\\java72\\images";
    public FileThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        System.out.println("socket连接建立了。。。" + s);
        OutputStream out=null;
        try {
             out = s.getOutputStream();
            InputStream in = s.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();
            System.out.println(s+"要下载："+fileName);
            FileInputStream fin=new FileInputStream(new File(Folder,fileName));
            byte []b=new byte[fin.available()];
            int len = fin.read(b);
            fin.close();

            out.write(b,0,len);
            out.flush();
            System.out.println("发送鸡汤"+s+"成功！");
        } catch (IOException e) {
            try {
                out.write(-1);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s+"关闭了。。。聊天结束");
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}
